<!--
 Copyright (c) 2016,2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
 All rights reserved.
 node-red-contrib-modbus - The BSD 3-Clause License

 @author <a href="mailto:klaus.landsdorf@bianco-royal.de">Klaus Landsdorf</a> (Bianco Royal)
-->
<script type="text/javascript">
  RED.nodes.registerType('modbus-flex-getter', {
    category: 'modbus',
    color: '#E9967A',
    defaults: {
      name: {value: ''},
      showStatusActivities: {value: false},
      showErrors: {value: false},
      showWarnings: {value: true},
      logIOActivities: {value: false},
      server: {type: 'modbus-client', required: true},
      useIOFile: {value: false},
      ioFile: {value: '', type: 'modbus-io-config', required: false},
      useIOForPayload: {value: false},
      emptyMsgOnFail: {value: false},
      keepMsgProperties: {value: false},
      delayOnStart: {value: false},
      startDelayTime: {value: ''},
    },
    inputs: 1,
    outputs: 2,
    align: "left",
    icon: 'modbus.png',
    paletteLabel: 'Modbus-Flex-Getter',
    label: function () {
      return this.name || 'Modbus Flex Getter'
    },
    oneditprepare: function () {

      let tabs = RED.tabs.create({
        id: "node-input-modbus-tabs",
        onchange: function (tab) {
          $("#node-input-tabs-content").children().hide()
          $("#" + tab.id).show()
        }
      })

      tabs.addTab({
        id: "modbus-settings-tab",
        label: this._("modbus-contrib.tabs-label.settings")
      })

      tabs.addTab({
        id: "modbus-options-tab",
        label: this._("modbus-contrib.tabs-label.options")
      })

      let useIOFileCheckbox = $('#node-input-useIOFile')
      let modbusiofileRow = $('#modbusiofile-row')

      useIOFileCheckbox.change(function () {
        if ($(this).is(':checked')) {
          modbusiofileRow.show()
        } else {
          modbusiofileRow.hide()
        }
      })

      if (this.delayOnStart) {
        $('#node-delay').show()
      }
      else {
        $('#node-delay').hide()
        $('#node-input-delayOnStart').prop('checked', false)
      }

      $('#node-input-delayOnStart').change(function () {
        if ($(this).is(':checked')) {
          $('#node-delay').show()
        } else {
          $('#node-delay').hide()
        }
      })
    }
  })
</script>


<script type="text/x-red" data-template-name="modbus-flex-getter">
    <div class="form-row">
        <ul style="min-width: 600px; margin-bottom: 20px;" id="node-input-modbus-tabs"></ul>
    </div>
    <div id="node-input-tabs-content" style="min-height: 170px;">
        <div id="modbus-settings-tab" style="display:none">
            <div class="form-row">
                <label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
                <input type="text" id="node-input-name" placeholder="Name">
            </div>
            <div class="form-row">
                <label for="node-input-server"><i class="icon-globe"></i> <span data-i18n="modbus-contrib.label.server"></span></label>
                <input type="text" id="node-input-server">
            </div>
            <div class="form-row" id="node-delayonstart">
                <label for="node-input-delayOnStart" style="width:160px">
                <i class="fa fa-power-off"></i> <span data-i18n="modbus-contrib.label.delayOnStart"></span></label>
                <input type="checkbox" id="node-input-delayOnStart" style="width:40px">
            </div>
            <div class="form-row" id="node-delay">
                <label for="node-input-startDelayTime"><i class="fa fa-clock-o"></i> <span data-i18n="modbus-contrib.label.startDelayTime"></span></label>
                <input type="text" id="node-input-startDelayTime" placeholder="10" style="width:80px">
            </div>
        </div>
        <div id="modbus-options-tab" style="display:none">
            <div class="form-row">
                <label style="min-width:190px" for="node-input-emptyMsgOnFail"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.emptyMsgOnFail"></span></label>
                <input type="checkbox" id="node-input-emptyMsgOnFail" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-keepMsgProperties"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.keepMsgProperties"></span></label>
                <input type="checkbox" id="node-input-keepMsgProperties" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showStatusActivities"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showActivities"></span></label>
                <input type="checkbox" id="node-input-showStatusActivities" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showErrors"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showErrors"></span></label>
                <input type="checkbox" id="node-input-showErrors" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showWarnings"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showWarnings"></span></label>
                <input type="checkbox" checked id="node-input-showWarnings" style="max-width:30px">
            </div>
            <hr>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-useIOFile"><i class="fa fa-file-code-o"></i> <span
                data-i18n="modbus-contrib.label.useIOFile"></span></label>
                <input type="checkbox" id="node-input-useIOFile" style="max-width:30px">
            </div>
            <div id="modbusiofile-row">
                <div class="form-row">
                    <label style="min-width:190px" for="node-input-logIOActivities"><i class="fa fa-th"></i> <span
                    data-i18n="modbus-contrib.label.logIOActivities"></span></label>
                    <input type="checkbox" id="node-input-logIOActivities" style="max-width:30px">
                </div>
                <div class="form-row">
                    <label style="min-width:190px" for="node-input-useIOForPayload"><i class="fa fa-comment-o"></i> <span data-i18n="modbus-contrib.label.useIOForPayload"></span></label>
                    <input type="checkbox" id="node-input-useIOForPayload" style="max-width:30px">
                </div>
                <div class="form-row">
                    <label for="node-input-ioFile"><i class="icon-file"></i> <span data-i18n="modbus-contrib.label.ioFile"></span></label>
                    <input type="text" id="node-input-ioFile">
                </div>
            </div>
        </div>
    </div>
</script>


<script type="text/x-red" data-help-name="modbus-flex-getter">
    <p>
        Modbus flexible input triggered read node with connection input parameters.
    </p>
    <p>Connects to a Modbus TCP or serial to read coils/inputs/registers at the rate of the incoming msg.</p>
    <p>
        <h4>Function codes (1:4) currently supported include:</h4>

        <ul>
          <li>FC 1: Read Coil Status</li>
          <li>FC 2: Read Input Status</li>
          <li>FC 3: Read Holding Registers</li>
          <li>FC 4: Read Input Registers</li>
        </ul>
    </p>

     <p>
         <strong>Input parameter for connecting Modbus</strong>
         <ul>
             <li>unitid (0..255 tcp | 0..247 serial) - overrides default Unit-ID</li>
             <li>fc (1..4)</li>
             <li>start address (0:65535)</li>
             <li>quantity (1:65535) of coils/inputs/registers to be read from the start address</li>
         </ul>
     </p>

     <p>Output 1: data Array (PDU), modbus response Buffer, input message</p>
     <p>Output 2: modbus response Buffer, data Array (PDU), input message</p>

     <p>Function node code example for single input:</p>
     <code>
        msg.payload = { value: msg.payload, 'fc': 1, 'unitid': 1, 'address': 0 , 'quantity': 1 }
        return msg
     </code>

     <p>Function node code example for multiple inputs:</p>
     <code>
        msg.payload = { value: msg.payload, 'fc': 3, 'unitid': 1, 'address': 0 , 'quantity': 10 }
        return msg
     </code>

     <p>
        The status of the Flex-Getter-Node is "active"  if the operating system opens up the dev with no error.
        Connection issues with your ethernet connection won't affect this status.
        Warnings and notifications about ethernet connection issues will be displayed in the Debug-window.
    </p>

    <p>
        To use the Flex-Getter-Node with "split"- and "join"-nodes, please make sure to enable "Keep Msg Properties" in the nodes' "Optionals" properties.
    </p>


</script>
